<?php
namespace Modules\Frontend\Controllers;

class UserBankInfoController extends \Phalcon\Mvc\Controller {
	public function beforeExecuteRoute() {
		\Components\Utils::isClosedFrontent($this);
	}

	public function initialize() {
		$this -> response -> setStatusCode(200, 'OK') -> sendHeaders() -> setContentType('application/json', 'UTF-8');
	}

	public function findFirstByIdAction($id) {
		$serviceUser = $this -> account::getServiceInstance() -> checkLogin($this -> request -> getQuery('token', 'string', null));
		if($serviceUser['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$serviceUser['code'],'msg'=>$serviceUser['message']]);
		}

		$service = $this -> account::getBankInfoServiceInstance() -> findFirstById($id);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>$service['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success', 'user_bank_info' => $service['user_bank_info']]);
	}

	public function indexAction() {
		$serviceUser = $this -> account::getServiceInstance() -> checkLogin($this -> request -> getQuery('token', 'string', null));
		if($serviceUser['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$serviceUser['code'],'msg'=>$serviceUser['message']]);
		}

		$service = $this -> account::getBankInfoServiceInstance() -> find([
			'conditions' => 'user_id = ' . $serviceUser['user'] -> user_id
		], 999, 1);
		if($service['code'] != 0) {
			//return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>$service['message']]);
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>'无银行卡数据,请添加']);
		}

		$userBankInfos = '';
		foreach($service['user_bank_info'] as $userBankInfo) {
			if($userBankInfo -> type == 1) {
				$userBankInfos .= $userBankInfo -> user_bank_info_id . ',';
			}
		}
		$userBankInfos = rtrim($userBankInfos, ',');
		if(!empty($userBankInfos)) {
			$countSellService = $this -> trade::getSellServiceInstance() -> count([
				'conditions' => 'from_user_id = ' . $serviceUser['user'] -> user_id . ' AND user_bank_info_id in(' . $userBankInfos . ')',
				'limit' => 3
			]);

			$pager = [];
			if($countSellService >= 2) {
				foreach($service['user_bank_info'] as $userBankInfo) {
					if($userBankInfo -> type != 1) {
						$pager[] = $userBankInfo;
					}
				}

				if(empty($pager)) {
					return $this -> response -> setJsonContent(['code'=>-1,'msg'=>'无可用收款方式,请添加!']);
				}

				return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success', 'pager' => $pager]);
			}
		}
		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success', 'pager' => $service['user_bank_info']]);
	}

	public function createAction() {
		$serviceUser = $this -> account::getServiceInstance() -> checkLogin($this -> request -> getQuery('token', 'string', null));
		if($serviceUser['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$serviceUser['code'],'msg'=>$serviceUser['message']]);
		}

		$data = $this -> request -> getPost();
		$data['bank_name'] = $data['bankName'];
		unset($data['bankName']);
		$data['bank_account'] = $data['bankAccount'];
		unset($data['bankAccount']);
		$data['bank_address_detail'] = $data['bankAddressDetail'];
		unset($data['bankAddressDetail']);
		$data['user_id'] = $serviceUser['user'] -> user_id;
		$data['create_time'] = time();

		$service = $this -> account::getBankInfoServiceInstance() -> create($data);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>$service['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'提交成功']);
	}

	public function updateAction($id) {
		$serviceUser = $this -> account::getServiceInstance() -> checkLogin($this -> request -> getQuery('token', 'string', null));
		if($serviceUser['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$serviceUser['code'],'msg'=>$serviceUser['message']]);
		}

		$data = $this -> request -> getPut();
		$data['bank_name'] = $data['bankName'];
		unset($data['bankName']);
		$data['bank_account'] = $data['bankAccount'];
		unset($data['bankAccount']);
		$data['bank_address_detail'] = $data['bankAddressDetail'];
		unset($data['bankAddressDetail']);

		$service = $this -> account::getBankInfoServiceInstance() -> update($id, $data);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>$service['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'提交成功']);
	}
}